package com.treemap;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;


public class TreeMapDemo {

	public static void main(String[] args){
		
	TreeMap<PersonId, Person> tm=new TreeMap<PersonId, Person>(new MyCompare());
	
	Person p1=new Person("Damu","kkvp");
	Person p2=new Person("Vinay","rjp");
	Person p3=new Person("Ahok","rly");
	Person p4=new Person("Damu","kkvp");
	Person p5=new Person("muni","nlr");
	Person p6=new Person("Vinay","rjp");
	
	PersonId pid1=new PersonId("p001");
	PersonId pid2=new PersonId("p002");
	PersonId pid3=new PersonId("p003");
	PersonId pid4=new PersonId("p001");
	PersonId pid5=new PersonId("p004");
	PersonId pid6=new PersonId("p002");
	tm.put(pid1, p1);
	tm.put(pid2, p2);
	tm.put(pid3, p3);
	tm.put(pid4, p4);
	tm.put(pid5, p5);
	tm.put(pid6, p6);
	System.out.println(tm);
	
	for(Map.Entry m:tm.entrySet()){  
		   PersonId pd=(PersonId) m.getKey();
		   Person p=(Person) m.getValue();
		   System.out.println(pd.getPid()+" "+p.getPname()+"     "+p.getPadd());  
		  }  
	}
}

class Person{
	private String  pname, padd;

	
	public String getPname() {
		return pname;
	}

	public String getPadd() {
		return padd;
	}

	public Person(String pname, String padd) {
	
		this.pname = pname;
		this.padd = padd;
	}
	
}

class PersonId{
	
	private String pid;

	public PersonId(String pid) {
	
		this.pid = pid;
	}

	public String getPid() {
		return pid;
	}

	
}

class MyCompare implements Comparator{
	
	@Override
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		PersonId p1=(PersonId)o1;
		String s1=p1.getPid();
		PersonId p2=(PersonId)o2;
		String s2=p2.getPid();
		
		int start=Integer.parseInt(s1.substring(s1.length()-1, s1.length()));
		int end=Integer.parseInt(s2.substring(s2.length()-1, s2.length()));
		if(start==end){
			return 0;
		}
		else
			if(start>end){
				return 1;
			}
			else
		return -1;
	}
}